
#include "AudioStorage.h"
#include "Tasks.h"

namespace smartvoice {

REGISTER_SERVICE("audio-storage", AudioStorage);

#define AUDIOSTORAGE_DATANAME "audio"
#define AUDIOSTORAGE_SEGMENT  "date"
#define AUDIOSTORAGE_KEY      "taskid"

/////////////////////////////////////////////////////////////////////////////

NEBULA_ERROR_CODE AudioStorage::OnInitialize()
{
	NEBULA_ERROR_CODE rc = NEBULA_ERR_SUCCESS;

	if ((rc = SetDataName(AUDIOSTORAGE_DATANAME)) != NEBULA_ERR_SUCCESS)
	{
		perror("SetDataName(%s) failed, return code is %d", AUDIOSTORAGE_DATANAME, rc);
		return rc;
	}

	if ((rc = SetDataAlias(AUDIOSTORAGE_SEGMENT, AUDIOSTORAGE_KEY)) != NEBULA_ERR_SUCCESS)
	{
		perror("SetDataAlias(%s, %s) failed, return code is %d", 
			AUDIOSTORAGE_SEGMENT, AUDIOSTORAGE_KEY, rc);
		return rc;
	}
	
	return InitializeStorage();
}

NEBULA_ERROR_CODE AudioStorage::OnStart()
{
	return StartStorage();
}

void AudioStorage::OnStop()
{
	StopStorage();
}

void AudioStorage::OnFinalize()
{
	FinalizeStorage();
}

ITask* AudioStorage::AllocateTask()
{
	return SessionTask::Allocator(this);
}

NEBULA_ERROR_CODE AudioStorage::OnAckTask (ITask* task)
{
	SessionTask* ptask = dynamic_cast<SessionTask*>(task);
	if (!ptask)
	{
		perror("Cast task to SessionTask failed!");
		return NEBULA_ERR_INVALID_ARGUMENT;
	}

	//clear audio data to reduce network transfer load
	ptask->m_audio.clear();
	
	return NEBULA_ERR_SUCCESS;
}

} // namespace smartvoice

